package controllers;

import java.util.List;

import play.data.validation.Required;
import play.mvc.Controller;

import models.Organization;
import models.RelationRequest;
import models.User;

public class RelationRequests extends Controller {
	/**
	 * @author Menna Magdy save in it the organizationId that we stand on
	 */
	public static long organizationIds;
	/**
	 * @author Menna Magdy save in it the user's permission type
	 */
	public static String permissionType;

	/**
	 * renders the page requestForRelation to show the application for the
	 * request
	 * 
	 * @param idForOrganization
	 *            the id of the organization
	 * @author Menna Magdy
	 */
	public static void requestForRelation(Long idForOrganization,
			String userType) {
		try{
		organizationIds = idForOrganization;
		permissionType = userType;
		render(idForOrganization, userType);
		}
		catch(NullPointerException error){
			Requests.contentNotFound();
		}
	}

	/**
	 * adds new relationRequest to the relationRequest List of the
	 * organizationLead then renders a page to confirm user that the request was
	 * sent to the organizationLead
	 * 
	 * @author Menna Magdy
	 * @param idForOrganization
	 *            the id of the organization
	 */
	public static void responseRelation(Long idForOrganization,
			String userType, String message) {
		try{
		organizationIds = idForOrganization;
		Organization organization = Organization.findById(organizationIds);
		List<RelationRequest> action = organization.receiveRelations;
		permissionType = userType;
		render(action, organizationIds, userType, message);
		}
		catch(NullPointerException error){
			Requests.contentNotFound();
		}
	
	}

	/**
	 * validate if the new relation name was valid and if the request was
	 * created or not by calling method addRelationRequest in the organization
	 * class, then renders a page to confirm user that the request was sent to
	 * the organizationLead
	 * 
	 * @param requestedName
	 *            the new relation name requested
	 * @author Menna Magdy
	 */
	public static void sendRelationRequest(String requestedName) {
        try{
		Organization organization = Organization.findById(organizationIds);
		String userIdString = session.get("userId");
		if (userIdString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(userIdString);
		User author = User.findById(userId);
		String userType = permissionType;
		if (validation.hasErrors()) {
			flash.error("Please enter the relation name!");
			requestForRelation(organizationIds, userType);
		}
		boolean flag = organization.addRelationRequest(author, requestedName);
		if (flag == false) {
			flash.error("This relation name is already existed!!");
			requestForRelation(organizationIds, userType);
		}
		String action = "Your Request has been sent to the Organization Lead!!";
		Relations.requestPage(action);
        }
		catch(NullPointerException error){
			Requests.contentNotFound();
		}
	}

	/**
	 * adds the relation name requested to the relationNames list of the
	 * organization then send notifications with a confirmation message to the
	 * sender, then renders a page to confirm the organizationLead that
	 * relationName was successfully added and confirmation was sent
	 * 
	 * @param idForOrganization
	 *            the id of the organization
	 * @author Menna Magdy
	 */
	public static void confirmRequest(Long idForRequest) {
		
		try{
		Organization organization = Organization.findById(organizationIds);
		RelationRequest request = RelationRequest.find("byId",
				idForRequest).first();
		organization.addRelationName(request.relationName);
		User sender = request.sender;
		String message = "Your relation name request '" + request.relationName
				+ " ' has been confirmed";
		request.organization.lead.replyRequest(sender, message);
		request.delete();
		String action = "You have successfully added'"+request.relationName+"' name and the confirmation message has been sent to " + sender.name;
		String userType = permissionType;
		responseRelation(organizationIds, userType, action);
		}
		catch(NullPointerException error){
			Requests.contentNotFound();
		}
	}

	/**
	 * calls the replyRequest method in User class with a rejection message to
	 * send it to the request's sender and delete the request from the
	 * organizationLead relationRequests List, then renders a page to confirm
	 * the organizationLead that message was already send and the request was
	 * deleted
	 * 
	 * @param idForOrganization
	 *            the id of the organization
	 * @author Menna Magdy
	 */
	public static void rejectRequest(Long idForRequest) {
		try{
		RelationRequest request = RelationRequest.find("byId",
				idForRequest).first();
		User sender = request.sender;
		String message = "Your relation name request '" + request.relationName
				+ " ' has been rejected";
		request.organization.lead.replyRequest(sender, message);
		String action = "The rejection for'"+request.relationName+"' name has been sent to " + sender.name;
		request.delete();
		String userType = permissionType;

		responseRelation(organizationIds, userType, action);
	}
	catch(NullPointerException error){
		Requests.contentNotFound();
	}
	}

}